package com.learning.cloud.controller;

import com.learning.cloud.entity.User;
import com.learning.cloud.service.UserService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 *
 * @author minghuiZhang
 * @date created in  14:40 2018/4/28
 * modified by
 */
@RestController
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private EurekaClient eurekaClient;
	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("/simple/{id}")
	public User findById(@PathVariable("id") Long id){
		return userService.findOne(id);
	}
	@GetMapping("/eureka-instance")
	public String serviceUrl() {
		InstanceInfo instance = eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
		return instance.getHomePageUrl();
	}

	@GetMapping("/eureka-info")
	public ServiceInstance showInfo() {
		return discoveryClient.getLocalServiceInstance();
	}

	@PostMapping("/user")
	public User findByIdPost(@RequestBody User  user){
		return user;
	}
}
